//
//  EventDescriptionFormatterSpec.swift
//  BDDExamplesSwift
//
//  Created by Xcode Server on 2017/4/26.
//  Copyright © 2017年 huoshuguang. All rights reserved.
//

import Quick
import Nimble
import OCMock
import MTDates

@testable import BDDExamplesSwift
///我们的目标是测试 EventDescriptionFormatter 是否返回像 "My Event starts at Aug 21, 2014, 12:00 AM and ends at Aug 21, 2014, 1:00 AM." 这样的格式化后的描述
class EventDescriptionFormatterSpec: QuickSpec
{
    override func spec()
    {
        //测试内部实现
        //这个例子并没有确切地测试 EventDescriptionFormatter 的行为。它主要通过 mock NSDateFormatter 来测试其内部实现
        describe("EventDescriptionFormatter") { 
            
            var descriptionFormatter:EventDescriptionFormatter?
            //
            beforeEach {descriptionFormatter = EventDescriptionFormatter()}
            afterEach {descriptionFormatter = nil}
            //两个用来检查格式化样式的测试用例
            it("日期格式:short", closure: {
                expect(descriptionFormatter?.dateFormatter?.dateStyle).to(equal(DateFormatter.Style.short))
            })
            
            it("时间格式:medium", closure: {
                //
                expect(descriptionFormatter?.dateFormatter?.timeStyle).to(equal(DateFormatter.Style.medium))
            })
            
            describe("通过 mock NSDateFormatter 来测试其内部实现") {
                var mockDateFormatter:Any?
                var eventDescription:String?
                var mockEvent:Event!
                
                beforeEach {
                     // 准备 mock date formatter
                     // 先 mock唯一依赖,组件内的时间格式化器 (DateFormatter)
                    mockDateFormatter = OCMStrictClassMock(for: DateFormatter.self)
                    //模拟固定的开始、结束时间:用mockDateFormatter返回开始和结束日期的固定字符串
                    descriptionFormatter?.dateFormatter = mockDateFormatter as! DateFormatter?
                    let startDate:NSDate = NSDate.mt_date(fromYear: 2014, month: 8, day: 21) as NSDate
                    let endDate:NSDate = startDate.mt_dateHours(after: 1) as NSDate
                //http://stackoverflow.com/questions/24041258/how-passing-a-protocol-as-parameter-in-swift/27162499#27162499
                    // 准备 mock Event事件
                    mockEvent = OCMStrictProtocolMock(for:Event.self) as? Event
//                    let proco:Protocol! = objc_getProtocol("BDDExamplesSwift.Event")
//                    mockEvent = OCMockObject.mock(for: proco)
                   //然后我们检查从Event事件的格式化器里,返回的字符串是否使用了我们先前 mock对象mockDateFormatter 的值。
                    OCMStub(mockEvent.startDate).andReturn(startDate)
                    OCMStub(mockEvent.endDate).andReturn(endDate)
                    
                    let start:NSString? = descriptionFormatter?.dateFormatter?.string(from: startDate as Date) as NSString?
                    let end:NSString? = descriptionFormatter?.dateFormatter?.string(from: endDate as Date) as NSString?
                    
                    OCMStub(start!).andReturn("ddd")
                    OCMStub(end!).andReturn("ddddf")
                    
                    eventDescription = descriptionFormatter?.eventDescriptionFromEvent(event: mockEvent)
                }
                
                it("验证开始", closure: {
                    //
                    expect(eventDescription).to(equal("Fixture Name starts at Fixture String 1 and ends at Fixture String 2."))
                })
                
            }
        }
    }
}
